import request from '@/utils/request'

export default {
  getSpecList(searchModel) {
    const encodedSearchKey = searchModel.search_key;
    const params = JSON.stringify({
      page: searchModel.page,
      pageSize: searchModel.pageSize,
      search_key: encodedSearchKey
    })
    console.log(params);
    // 创建 URLSearchParams 对象并设置参数
    return request({
      url: '/planting-tree-specification/pageList',
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      },
      data: params
      // params
    });
  },
  // 用户管理 分页列表
  // export function specPageList(data) {
  //   return request({
  //     url: '/spec/pageList',
  //     method: 'post',
  //     data
  //   })
  // },

  // 添加
  addSpec(spec) {
    return request({
      url: '/planting-tree-specification/add',
      method: 'post',
      data: spec
    });
  },

  // 更新树种规格
  updateSpec(spec) {
    return request({
      url: '/planting-tree-specification/update',
      method: 'post',
      // ...spec
      data: { donate_amount: spec.donate_amount, memo: spec.memo, name: spec.name, id: spec.id, planting_tree_id: spec.planting_tree_id, sort: spec.sort, status: spec.status }
    });
  },

  // 保存
  saveSpec(spec) {
    // if(spec.id == null && spec.id == undefined){
    //   return this.addSpec(spec);
    // }
    // 添加
    if (!spec.id) {
      return this.addSpec(spec);
    }
    return this.updateSpec(spec);
  },

  deleteSpecById(id) {
    return request({
      url: `/planting-tree-specification/delete`,
      method: 'post',
      data: { id }
    });
  },
}
